import Vue from 'vue';
let vm = new Vue();
import axios from 'axios'
import qs from 'qs';

import Config from './config'
import Utils from './utils'
import User from './user'
import Router from '../router'

// export default {
const http = {
    httpPost:function(obj){
        // let _datas = Utils.isUndefined(obj.datas) ? null : qs.stringify(obj.datas);
        let _datas = null;
        if(Utils.isUndefined(obj.datas) && !User.getToken()){
            _datas = null;
        }else{
            _datas = {};
            if(!Utils.isUndefined(obj.datas)){
                _datas = obj.datas;
            }

            let _token = User.getToken();
            if(_token){
                _datas.token = _token;
            }

            _datas = qs.stringify(_datas);
        }
        let _showLoad = Utils.isUndefined(obj.showLoad) ? true : obj.showLoad;
        let _heads = {'Content-Type':'application/x-www-form-urlencoded','Accept': 'application/json'};

        let loadingInstance = null;
        if(_showLoad) {
            loadingInstance = vm.$loading({lock:true});
        }


        // console.log(obj.url);

        // if(obj.url !== '/api/auth/login'){
        //     _heads.Authorization = User.getToken();
        // }

        // console.log(_heads);
        axios({
            headers:_heads,
            method: 'post',
            url: obj.url.indexOf('http') === 0 ? obj.url : Config.apiUrl+obj.url,
            data:_datas
        }).then(function(res){
            if(loadingInstance){
                vm.$nextTick(() => { // 以服务的方式调用的 Loading 需要异步关闭
                    loadingInstance.close();
                });
            }
            if(res.data.status === false){
                if (obj.fail) {
                    obj.fail(res.data);
                } else {
                    vm.$message.error({message: res.data.message, duration: 2000});
                }
            }else{
                if(res.data.httpCode === 200){
                    if(obj.success){
                        obj.success(res.data.data);
                    }
                }else{
                    if (obj.fail) {
                        obj.fail(res.data);
                    } else {
                        vm.$message.error({message: res.data.message, duration: 2000});
                    }
                }
            }
        },function(error){
            vm.$message.error(error);
        }).catch(function (error) {
            console.log(error);
            vm.$message.error(error.toString());
            if(loadingInstance){
                vm.$nextTick(() => { // 以服务的方式调用的 Loading 需要异步关闭
                    loadingInstance.close();
                });
            }
        })
    },
    upLoad:function(file){
        let config = {
            //添加请求头
            headers: { "Content-Type": "multipart/form-data" },
            //添加上传进度监听事件
            onUploadProgress: e => {
                var completeProgress = ((e.loaded / e.total * 100) | 0) + "%";
                this.progress = completeProgress;
            }
        };

        let param = new FormData();
        param.append("imageFile", file);
        console.log(param.get("imageFile"));

        axios.post('http://miiheapi/user/up', param, config).then(
            function (response)
            { console.log(response); })
            .catch(function (error) {
                console.log(error);
            });
    }
};

export default http;
